/**
 * Proyecto: ProyectoEscom
 * por: SerGenis (05/06/2012 17:45:08).
 * ##############################################
 */
package com.sergenis.swing;

import com.sergenis.beans.AbstractBean;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.List;
import java.util.Vector;

import javax.swing.AbstractListModel;
import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListModel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import sun.security.x509.AttributeNameEnumeration;

/**
 * @author SerGenis (05/06/2012 17:45:08)
 */
public class JMultiViewList<E>
    extends AbstractBean {

    public JMultiViewList() {
	this(new java.util.Vector<E>());
    }

    public JMultiViewList(Vector<E> elements) {
	this.listmodel = new ListViewModel<E>(elements);
	this.listview = new JListView<E>(listmodel);
	this.scrollPane = new JScrollPane(listview);
    }
    private ListViewType viewType = ListViewType.CONTENT;
    private final JScrollPane scrollPane;
    private final JListView<E> listview;
    private ListModel<E> listmodel;

    public ListViewType getViewType() {
	return viewType;
    }

    public void setViewType(ListViewType viewType) {
	ListViewType oldValue = this.viewType;
	this.viewType = viewType;
	listview.setLayoutOrientation(viewType.getOrientation());
	firePropertyChange("viewType", oldValue, this.viewType);
    }

    public void setElements(Vector<E> elements) {
	ListModel oldModel = this.listmodel;
	this.listmodel = new ListViewModel<E>(elements);
	listview.setModel(this.listmodel);
	firePropertyChange("elements", oldModel, viewType);
    }

    public JListView<E> getListview() {
	return listview;
    }

    public JScrollPane getScrollPane() {
	return scrollPane;
    }

    public final class JListView<T>
	extends JList<T> {

	public JListView() {
	}

	public JListView(Vector<? extends T> listData) {
	    super(listData);
	}

	public JListView(T[] listData) {
	    super(listData);
	}

	public JListView(ListModel<T> dataModel) {
	    super(dataModel);
	}

	@Override
	public int getScrollableUnitIncrement(Rectangle visibleRect,
	    int orientation, int direction) {
	    int row;
	    if (orientation == SwingConstants.VERTICAL && direction < 0
		&& (row = getFirstVisibleIndex()) != -1) {
		Rectangle r = getCellBounds(row, row);
		if ((r.y == visibleRect.y) && (row != 0)) {
		    Point loc = r.getLocation();
		    loc.y--;
		    int prevIndex = locationToIndex(loc);
		    Rectangle prevR = getCellBounds(prevIndex, prevIndex);
		    if (prevR == null || prevR.y >= r.y) return 0;
		    return prevR.height;
		}
	    }
	    return super.getScrollableUnitIncrement(visibleRect, orientation,
		direction);
	}
    }

    private final class ListViewModel<V>
	extends AbstractListModel<V> {

	public ListViewModel(java.util.List<V> listData) {
	    this.listData = listData;
	}
	private final java.util.List<V> listData;

	public void add(int index, V element) {
	    listData.add(index, element);
	    fireIntervalAdded(this, index, index);
	}

	public int getSize() {
	    return this.listData.size();
	}

	public V getElementAt(int index) {
	    return listData.get(index);
	}
    }
}
